package com.rongc.client.freight.business.message.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseFragment;
import com.rongc.client.freight.base.view.widget.DividerItemDecoration;
import com.rongc.client.freight.business.message.model.MessageBean;
import com.rongc.client.freight.business.message.request.api.MessageApi;
import com.rongc.client.freight.business.message.view.adapter.RecyclerMessageAdapter;
import com.rongc.client.freight.utils.UtilMethod;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by Bruce on 2016/11/24.
 */
public class MessageLeftFragment extends BaseFragment {
    @Bind(R.id.recycler)
    RecyclerView mRvBot;

    RecyclerMessageAdapter mLvAdapter;
    List<MessageBean> lsNews;

    public static MessageLeftFragment getInstance(){
        /*if(fragment == null) fragment = new HomeFragment(orderType);
        return fragment;*/
        MessageLeftFragment fragment = new MessageLeftFragment();
        Bundle bdl = new Bundle();
//        bdl.putString("orderType", orderType);
        fragment.setArguments(bdl);
        return fragment;
    }


    public void setDatas(List<MessageBean> datas){
        this.lsNews = datas;
    }

    Response.Listener<MessageBean[]> respOrderListener = new Response.Listener<MessageBean[]>() {
        @Override
        public void onResponse(MessageBean[] response) {
            if (HandleCode.requestSuccess()) {
                lsNews.clear();
                if(response != null)
                    for (int i=0;i<response.length;i++){
                        lsNews.add(response[i]);
                    }
                mLvAdapter.notifyDataSetChanged();
            } else {
                UtilMethod.dismissProgressDialog(getActivity());
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            LogUtils.i("error.getMessage(): " + error.getMessage());
            UtilMethod.dismissProgressDialog(getActivity());
        }
    };


    @Override
    public void initData() {
        RequestManager.getInstance().call(new MessageApi(
                new MessageApi.MessageParams(),
                respOrderListener, errorListener));
    }

    @Override
    public void initListener() {

    }


    @Override
    public void initView() {
        lsNews = new ArrayList<>();

        mLvAdapter = new RecyclerMessageAdapter(getActivity(), lsNews);
        mRvBot.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
        mRvBot.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        mRvBot.setAdapter(mLvAdapter);
    }


    @Override
    public int getLayoutId() {
        return R.layout.universal_recycler_2;
    }
}
